package com.vicki.music;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.Random;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;

public class MediaPlaybackService extends Service {

	public static final String MediaPlaybackLog = "MediaPlaybackService";
	public static final int REPEAT_CURRENT = 1;
	public static final int REPEAT_ALL = 2;
	public static final int REPEAT_OFF = 0;
	public static int REPEAT_MODE = REPEAT_OFF;

	public static final int SHUFFLE_ON = 1;
	public static final int SHUFFLE_OFF = 0;
	public static int SHUFFLE_MODE = SHUFFLE_OFF;

	private int current_play_positon = 1;

	private MediaPlayer mMediaPlayer;

	private String[] mPlayList;

	private LinkedList<String> mRecentList = new LinkedList<String>();

	@Override
	public IBinder onBind(Intent intent) {

		init();
		return serviceStub;
	}

	private void init() {
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.Media.DATA }, null, null, null);
		cursor.moveToFirst();

		mPlayList = new String[cursor.getCount()];

		for (int i = 0; i < cursor.getCount(); i++) {
			mPlayList[i] = cursor.getString(0);
			cursor.moveToNext();
		}
		cursor.close();
		Log.i(MediaPlaybackLog, "mPlayList size is " + mPlayList.length);
		showPlayList();
	}

	private long duration() {
		return 0;
	}

	private long getAlbumId() {

		return 0;
	}

	private String getAlbumName() {

		return null;
	}

	private long getArtistId() {

		return 0;
	}

	private String getArtistName() {

		return null;
	}

	private long getAudioId() {

		return 0;
	}

	private String getPath() {

		return null;
	}

	private int getRepeatMode() {

		return REPEAT_MODE;
	}

	private int getShuffleMode() {

		return SHUFFLE_MODE;
	}

	private boolean isPlaying() {

		return false;
	}

	private void setPlaylist(String playlist_type) {
		// MediaSqlite mediaSqlite = new MediaSqlite(this);
		// Cursor cursor = mediaSqlite.query(playlist_type, 0);
		// int count = cursor.getCount();
		// cursor.moveToFirst();
		// for (int i = 0; i < count; i++) {
		// MediaInfo mediaInfo = new MediaInfo(cursor.getString(0), cursor
		// .getString(3), cursor.getString(1), cursor.getString(6),
		// cursor.getString(4), cursor.getString(2), cursor
		// .getString(5));
		//
		// mPlayList.add(mediaInfo._path);
		// cursor.moveToNext();
		// }
		// cursor.close();
	}

	private void pause() {
		if (mMediaPlayer == null)
			return;
		mMediaPlayer.pause();
	}

	private void play() {
		Log.i(MediaPlaybackLog, "mCurrentPlayPos is " + current_play_positon);
		if (mMediaPlayer == null){
			mMediaPlayer = new MediaPlayer();
			try {
				mMediaPlayer.setDataSource(mPlayList[current_play_positon]);
				Log.i(MediaPlaybackLog, mPlayList[current_play_positon]);
				mMediaPlayer.prepare();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if (mMediaPlayer.isPlaying()) {
			mMediaPlayer.pause();
		} else {
			mMediaPlayer.start();
		}
	}

	private long position() {

		return 0;
	}

	private void next() {
		if (mMediaPlayer == null)
			mMediaPlayer = new MediaPlayer();
		mMediaPlayer.release();
		mMediaPlayer = null;

		if (SHUFFLE_MODE == SHUFFLE_ON) {
			int pos = random();
			current_play_positon = pos;
		} else {
			if (REPEAT_MODE != REPEAT_CURRENT) {
				boolean last = is_last();
				if (last) {
					if (REPEAT_MODE == REPEAT_ALL) {
						current_play_positon = 0;
					} else if (REPEAT_MODE == REPEAT_OFF) {
						current_play_positon = mPlayList.length - 1;
					}
				} else {
					current_play_positon++;
				}

			}
		}
		play();
	}

	private boolean is_last() {
		final int currentPos = current_play_positon;
		final int playlist_size = mPlayList.length;
		if (currentPos == playlist_size - 1)
			return true;
		return false;
	}

	private int random() {
		final int playlist_size = mPlayList.length;
		Random random = new Random();
		return random.nextInt(playlist_size);
	}

	private void prev() {
		if (mMediaPlayer == null)
			mMediaPlayer = new MediaPlayer();
		mMediaPlayer.release();
		mMediaPlayer = null;

		if (SHUFFLE_MODE == SHUFFLE_ON) {
			int pos = random();
			current_play_positon = pos;
		} else {
			if (REPEAT_MODE != REPEAT_CURRENT) {
				boolean last = is_first();
				if (last) {
					if (REPEAT_MODE == REPEAT_ALL) {
						current_play_positon = mPlayList.length-1;
					} else if (REPEAT_MODE == REPEAT_OFF) {
						current_play_positon = 0;
					}
				} else {
					current_play_positon--;
				}

			}
		}
		play();
	}

	private boolean is_first() {
		final int currentPos = current_play_positon;
		if (currentPos == 0)
			return true;
		return false;
	}

	private long seek(long pos) {

		return 0;
	}

	private void setRepeatMode() {
		REPEAT_MODE++;
		if (REPEAT_MODE > REPEAT_ALL) {
			REPEAT_MODE = REPEAT_OFF;
		} else if (REPEAT_MODE < REPEAT_OFF) {
			REPEAT_MODE = REPEAT_ALL;
		}
		Log.i(MediaPlaybackLog, "repeat_mode is " + REPEAT_MODE);
	}

	private void setShuffleMode() {
		if (SHUFFLE_MODE == SHUFFLE_OFF) {
			SHUFFLE_MODE = SHUFFLE_ON;
		} else {
			SHUFFLE_MODE = SHUFFLE_OFF;
		}
		Log.i(MediaPlaybackLog, "shufflemode is " + SHUFFLE_MODE);
	}

	private void stop() {

	}

	public String getTrackName() {
		return null;
	}

	private final IBinder serviceStub = new ServiceStub(this);

	/*
	 * By making this a static class with a WeakReference to the Service, we
	 * ensure that the Service can be GCd even when the system process still has
	 * a remote reference to the stub.
	 */
	static class ServiceStub extends IMediaPlaybackService.Stub {
		WeakReference<MediaPlaybackService> mService;

		ServiceStub(MediaPlaybackService service) {
			mService = new WeakReference<MediaPlaybackService>(service);
		}

		public boolean isPlaying() {
			return mService.get().isPlaying();
		}

		public void stop() {
			mService.get().stop();
		}

		public void pause() {
			mService.get().pause();
		}

		public void play() {
			mService.get().play();
		}

		public void prev() {
			mService.get().prev();
		}

		public void next() {
			mService.get().next();
		}

		public String getAlbumName() {
			return mService.get().getAlbumName();
		}

		public long getAlbumId() {
			return mService.get().getAlbumId();
		}

		public String getArtistName() {
			return mService.get().getArtistName();
		}

		public long getArtistId() {
			return mService.get().getArtistId();
		}

		public String getPath() {
			return mService.get().getPath();
		}

		public long getAudioId() {
			return mService.get().getAudioId();
		}

		public long position() {
			return mService.get().position();
		}

		public long duration() {
			return mService.get().duration();
		}

		public long seek(long pos) {
			return mService.get().seek(pos);
		}

		public void setShuffleMode() {
			mService.get().setShuffleMode();
		}

		public int getShuffleMode() {
			return mService.get().getShuffleMode();
		}

		public void setRepeatMode() {
			mService.get().setRepeatMode();
		}

		public int getRepeatMode() {
			return mService.get().getRepeatMode();
		}

	}

	private void showPlayList() {
		for (int i = 0; i < mPlayList.length; i++) {
			Log.i(MediaPlaybackLog, mPlayList[i] + "\n");
		}
	}
}
